package com.demo.order.controller;
import java.math.BigDecimal;


import com.alibaba.csp.sentinel.annotation.SentinelResource;

import com.demo.order.bean.Order;
import com.demo.order.properties.OrderProperties;
import com.demo.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

//@RefreshScope
//@CrossOrigin
@RestController
@Slf4j
//@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

//    @Value("${order.timeout}")
//    String orderTimeout;
//    @Value("${order.auto-confirm}")
//    String orderAutoConfirm;
    @Autowired
    private OrderProperties orderProperties;

    @GetMapping("/config")
    public String config(){
        return "order.timeout="+orderProperties.getTimeout()+"; order.auto-confirm="+orderProperties.getAutoConfirm();
    }
    //创建订单
    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId){
        Order order=orderService.createOrder(productId,userId);
        return order;

    }
    //秒杀订单
    @GetMapping("/seckill")
    @SentinelResource(value = "seckill-order",blockHandler = "seckillFallback")
    public Order seckill(@RequestParam(value = "userId",required = false) Long userId,
                             @RequestParam(value = "productId",defaultValue = "1000") Long productId){
        Order order=orderService.createOrder(productId,userId);
        order.setId(Long.MAX_VALUE);
        return order;

    }
    public Order seckillFallback(Long userId, Long productId, Throwable exception){
        log.info("seckillFallback...");
        Order order=new Order();
        order.setId(productId);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("");
        order.setAddress("异常信息："+exception.getMessage());


        return order;

    }
}
